package cn.weakup.designpattern.c_structural.decorator.usepattern;

import cn.weakup.designpattern.c_structural.decorator.HandlerInterceptor;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author lixj
 * @version 1.0.0
 * @date 2022/6/4
 * @desc
 */
public class LoginSsoDecorator extends SsoDecorator {
    private static Map<String, String> authMap = new
            ConcurrentHashMap<String, String>();
    static {
        authMap.put("developer", "queryUserInfo");
        authMap.put("commonUser", "queryUserInfo");
    }

    public LoginSsoDecorator(HandlerInterceptor handlerInterceptor) {
        super(handlerInterceptor);
    }

    /**
     * 重写SsoInterceptor的登录逻辑，以达到后续的逻辑变动
     * @param request
     * @param response
     * @param handler
     * @return
     */
    @Override
    public boolean preHandle(String request, String response, Object handler) {
        // 模拟校验
        boolean success = super.preHandle(request, response, handler);

        if (!success) {
            return false;
        }
        String userId = request.substring(8);
        String method = authMap.get(userId);

        System.out.println("模拟单点登录⽅法访问拦截校验：" +  userId + "  " + method);
        // 模拟⽅法校验
        return "queryUserInfo".equals(method);
    }
}
